package net.adst.workers;

import net.adst.beans.User;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.util.DataManager;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 01.11.12
 * Time: 6:38
 * To change this template use File | Settings | File Templates.
 */
    public class AuthenticationWorker extends SwingWorker<Boolean, Void> {
    private JDialog dialog;
    private JLabel label;
    private String name;
    private String password;

    public AuthenticationWorker(JDialog dialog, JLabel label, String name, String password) {
        this.dialog = dialog;
        this.label = label;
        this.name = name;
        this.password = password;
    }

    @Override
    protected Boolean doInBackground() throws RemoteException {
        try {
            label.setText("соединение...");
            label.setForeground(Color.black);
            InputStream in = AuthenticationWorker.class.getClassLoader().getSystemResourceAsStream("info.xls");
            HSSFWorkbook wb = new HSSFWorkbook(in);
            ExcelExtractor extractor = new ExcelExtractor(wb);
            extractor.setFormulasNotResults(false); // Считать формулы
            HSSFSheet sheet = wb.getSheet("люди");
            User user = null;
            for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
                if (sheet.getRow(i) != null) {
                    if (sheet.getRow(i).getCell(1).toString().equalsIgnoreCase(name)) {
                        if (!sheet.getRow(i).getCell(2).toString().isEmpty() && sheet.getRow(i).getCell(2) != null) {
                            if ((sheet.getRow(i).getCell(2).toString()).equals(password.hashCode() + ".0")) {
                                user = new User(name, password);
                            } else {
                                break;
                            }
                        } else {
                            user = new User(name, "");
                        }
                        if (sheet.getRow(i).getCell(3).toString() != null) {
                            String role = sheet.getRow(i).getCell(3).toString();
                            for (int k = 0; k < Constants.adm.length; k++) {
                                if (role.equalsIgnoreCase(Constants.adm[k])) {
                                    user.setAdmin(true);
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            DataManager.setObject(VariableNames.USER, user);
            if (user != null) {
                return true;
            } else {
                return false;
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return false;
        }
    }

    @Override
    public void done() {
        try {
            if (!get()) {
                label.setText("Неверно введен логин или пароль.");
                label.setForeground(Color.RED);
            } else {
                if (dialog != null)
                    dialog.dispose();
            }

        } catch (Exception e) {
            label.setText("Ошибка соединения");
            label.setForeground(Color.RED);
        }
    }
}
